package csvm_v2.components;

import csvm_v1.exceptions.WrongCommandFormatException;
import csvm_v1.products.CocaCola;
import csvm_v1.products.CustomProduct;
import csvm_v1.products.Fanta;
import csvm_v1.products.Product;
import csvm_v1.products.Sprite;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;

/**
 * membaca data vending machine yang tertulis dalam dump file
 *
 * @author Muqtafi Akhmad
 */
public class DumpFileReader {

    /**
     * reference stok koin
     */
    private CoinContainer cStock;
    /**
     * reference stok produk
     */
    private ProductContainer pStock;
    /**
     * reference product selector
     */
    private ProductSelector pSelector;

    /**
     * konstruktor dari DumpFileReader
     *
     * @param coinStock stok koin
     * @param productStock stok produk
     * @param productSelector product selector
     */
    public DumpFileReader(CoinContainer coinStock, ProductContainer productStock, ProductSelector productSelector) {
        this.cStock = coinStock;
        this.pStock = productStock;
        this.pSelector = productSelector;
    }

    /**
     * membaca data dari dump file
     *
     * @param fileName nama file
     * @throws WrongCommandFormatException
     */
    public void importDump(String fileName) throws WrongCommandFormatException, FileNotFoundException {
            String command;
            Scanner fileReader = new Scanner(new File(fileName));
            HashMap<Integer, Product> catalog = new HashMap<>();
            HashMap<Product, Integer> productStock = new HashMap<>();
            // init catalog dan product stock, baca dari file
            // membaca header produk
            command = fileReader.nextLine();
            if (!command.equals("#products")) {
                throw (new WrongCommandFormatException("Tidak ditemukan header data produk"));
            }
            // membaca jumlah product dari file
            int i, jumlahProduct = Integer.parseInt(fileReader.nextLine());
            for (i = 1; i <= jumlahProduct; i++) {
                // membaca product satu per satu
                command = fileReader.nextLine();
                // format line untuk fixed product : <nama : {CocaCola, Fanta, Sprite}> <jumlah>
                // format line untuk custom product : <nama> <select index> <harga> <jumlah>
                String commandArgs[] = command.split(" ");
                switch (commandArgs[0]) {
                    case "CocaCola": {
                        CocaCola cola = new CocaCola();
                        catalog.put(cola.getSelectIndex(), cola);
                        productStock.put(cola, Integer.parseInt(commandArgs[1]));
                        break;
                    }
                    case "Fanta": {
                        Fanta fanta = new Fanta();
                        catalog.put(fanta.getSelectIndex(), fanta);
                        productStock.put(fanta, Integer.parseInt(commandArgs[1]));
                        break;
                    }
                    case "Sprite": {
                        Sprite sprite = new Sprite();
                        catalog.put(sprite.getSelectIndex(), sprite);
                        productStock.put(sprite, Integer.parseInt(commandArgs[1]));
                        break;
                    }
                    default: {
                        // custom product
                        CustomProduct produk = new CustomProduct(Integer.parseInt(commandArgs[2]), Integer.parseInt(commandArgs[1]), commandArgs[0]);
                        catalog.put(produk.getSelectIndex(), produk);
                        productStock.put(produk, Integer.parseInt(commandArgs[3]));
                        break;
                    }
                }
            }
            // membaca koin 
            // membaca header koin
            command = fileReader.nextLine();
            if (!command.equals("#coins")) {
                throw (new WrongCommandFormatException("Tidak ditemukan header data koin"));
            }
            int jumlahCoin = 3;
            for (i = 1; i <= jumlahCoin; i++) {
                command = fileReader.nextLine();
                // format line untuk coin : <nama> <jumlah>
                String commandArgs[] = command.split(" ");
                switch (commandArgs[0]) {
                    case "Dime": {
                        cStock.setDimeCount(Integer.parseInt(commandArgs[1]));
                        break;
                    }
                    case "Nickel": {
                        cStock.setNickelCount(Integer.parseInt(commandArgs[1]));
                        break;
                    }
                    case "Quarter": {
                        cStock.setQuarterCount(Integer.parseInt(commandArgs[1]));
                        break;
                    }
                    default: {
                        // do nothing
                        break;
                    }
                }
            }
            pStock.setProductStock(productStock);
            pSelector.setCatalog(catalog);
        
    }
}
